package com.tuangou.tuangouheader.header.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.atguigu.common.utils.R;
import com.github.pagehelper.PageInfo;
import com.tuangou.tuangouheader.header.entity.vo.GroupRecord;
import com.tuangou.tuangouheader.header.entity.vo.GroupUserOrderEntityVo;
import com.tuangou.tuangouheader.header.entity.vo.GroupRecordEntityVo;
import com.tuangou.tuangouheader.header.entity.vo.GroupXq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tuangou.tuangouheader.header.entity.GroupRecordEntity;
import com.tuangou.tuangouheader.header.service.GroupRecordService;



/**
 * 
 *
 * @author zjb
 * @email zjb@tuangou.com
 * @date 2021-06-17 21:08:21
 */
@RestController
@RequestMapping("header/grouprecord")
public class GroupRecordController {
    @Autowired
    private GroupRecordService groupRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("header:grouprecord:list")
    public PageInfo<GroupRecordEntityVo> list(@RequestParam Map<String, String> params){
        return groupRecordService.list(params);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("header:grouprecord:info")
    public R info(@PathVariable("id") Integer id){
		GroupRecordEntity groupRecord = groupRecordService.getById(id);

        return R.ok().put("groupRecord", groupRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("header:grouprecord:save")
    public R save(@RequestBody GroupRecordEntity groupRecord){
		groupRecordService.save(groupRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("header:grouprecord:update")
    public R update(@RequestBody GroupRecordEntity groupRecord){
		groupRecordService.updateById(groupRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("header:grouprecord:delete")
    public R delete(@RequestBody Integer[] ids){
		groupRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 拼团购买商品
     */
    @RequestMapping("/buygoods")
    public R buygoods(@RequestParam("rid") int rid,
                      @RequestParam("uid") int uid){
        return groupRecordService.updategoods(rid,uid);
    }

    /**
     * 商品详情
     */
    @RequestMapping("/findListByGid")
    public List<GroupUserOrderEntityVo> findListByGid(@RequestParam("gid")int gid){
        return groupRecordService.findListByGid(gid);
    }

    /**
     * 团长发起拼团
     */
    @RequestMapping("/pintuan")
    //@RequiresPermissions("header:grouprecord:save")
    public R pintuan(@RequestBody GroupRecordEntity groupRecord){
        groupRecord.setTzId(2);
        groupRecord.setCreateDate(new Date());
        groupRecord.setGroupId(1);
        /*GroupGoodsEntity groupGoodsEntity = groupGoodsService.listById(groupRecord.getGroupId());*/
        groupRecord.setCurrentPeonum(0);
        groupRecord.setStatus(0);
        if (StringUtils.isEmpty(groupRecord.getTzId())){
            return R.error("团长不能为空");
        }
        if (StringUtils.isEmpty(groupRecord.getGroupId())){
            return R.error("拼团商品不能为空");
        }
        groupRecordService.save(groupRecord);
        return R.ok();
    }

    /**
     * 拼团列表
     */
    @RequestMapping("/findAll")
    public R findAll(){
        List<GroupRecord> list = groupRecordService.findAll();
        return R.ok().put("list",list);
    }


    /**
     * 拼团详情
     */
    @RequestMapping("/xianqing/{id}")
    public R xianqing(@PathVariable("id")Integer id){
        List<GroupXq> list = groupRecordService.xianqing(id);
        return R.ok().put("list",list);
    }
}
